package com.esri.core.internal.io.handler;

import com.esri.core.io.EsriSecurityException;
import com.esri.core.io.UserCredentials;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/* loaded from: classes.dex */
public class n {
    public String a;
    public final UserCredentials b;
    private static final String[] d = {"/MapServer", "/FeatureServer", "/ImageServer"};
    private static final Map<String, String> e = Collections.synchronizedMap(new a());
    public static final Map<String, UserCredentials> c = new a();

    /* loaded from: classes.dex */
    static class a<K, V> extends LinkedHashMap<K, V> {
        private static final int a = 10;
        private static final long serialVersionUID = 1;

        a() {
        }

        @Override // java.util.LinkedHashMap
        protected boolean removeEldestEntry(Map.Entry<K, V> entry) {
            return size() > 10;
        }
    }

    private n(String str, UserCredentials userCredentials) throws EsriSecurityException {
        this.b = userCredentials;
        StringBuilder sb = new StringBuilder(str);
        b(sb, userCredentials);
        this.a = sb.toString();
        a(this);
    }

    public static n a(String str, UserCredentials userCredentials) throws EsriSecurityException {
        if (str == null || str.trim().length() <= 0 || userCredentials == null || userCredentials.isEmpty()) {
            return null;
        }
        return new n(str, userCredentials);
    }

    public static void a(String str) throws EsriSecurityException {
        c.a(str, (UserCredentials) null);
    }

    private static final void a(StringBuilder sb) {
        String[] strArr;
        String sb2 = sb.toString();
        for (String str : d) {
            if (sb.indexOf(str) != -1) {
                if (!sb2.matches(".+" + str + "/\\d+.*")) {
                    return;
                }
                int lastIndexOf = sb.lastIndexOf(str);
                if (lastIndexOf > -1) {
                    sb.delete(lastIndexOf + str.length(), sb.length());
                    return;
                }
            }
        }
    }

    private static final void a(StringBuilder sb, UserCredentials userCredentials) {
        if (userCredentials.isSSLRequired() && sb.indexOf("http:") == 0) {
            sb.replace(0, 5, "https:");
        }
        if (sb.indexOf("https:") == 0) {
            userCredentials.setSSLRequired(true);
        }
    }

    public final String b(String str) {
        if (str == null) {
            return str;
        }
        StringBuilder sb = new StringBuilder(str);
        a(sb, this.b);
        return sb.toString();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(boolean z) {
        this.b.setSSLRequired(z);
        StringBuilder sb = new StringBuilder(this.a);
        a(sb, this.b);
        this.a = sb.toString();
    }

    private static final void b(StringBuilder sb, UserCredentials userCredentials) throws EsriSecurityException {
        a(sb);
        a(sb, userCredentials);
    }

    private static UserCredentials.AuthenticationType a(n nVar) throws EsriSecurityException {
        UserCredentials userCredentials = nVar.b;
        String str = nVar.a;
        UserCredentials.AuthenticationType authenticationType = userCredentials.getAuthenticationType();
        if (authenticationType != null) {
            return authenticationType;
        }
        UserCredentials.AuthenticationType d2 = c.d(str);
        if (d2 == UserCredentials.AuthenticationType.CERTIFICATE) {
            throw new EsriSecurityException((int) EsriSecurityException.MISSING_CLIENT_CERTIFICATE);
        }
        userCredentials.setAuthenticationType(d2);
        if (d2 == UserCredentials.AuthenticationType.TOKEN) {
            c.a(nVar, 1);
        }
        return d2;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void c(String str) {
        if (str != null) {
            this.b.setTokenServiceUrl(str);
            e.put(this.a, str);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final String a() {
        String tokenServiceUrl = this.b.getTokenServiceUrl();
        String str = e.get(this.a);
        if (str != null && tokenServiceUrl == null) {
            this.b.setTokenServiceUrl(str);
        }
        return this.b.getTokenServiceUrl();
    }
}
